package com.washmadam.service.domain.communicate;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.washmadam.service.common.enums.CommunicateTypeEnum;
import com.washmadam.service.common.enums.ReserveStatusEnum;
import com.washmadam.service.db.dto.CustomerOrder;
import com.washmadam.service.db.dto.CustomerOrderCommunicate;
import com.washmadam.service.db.dto.CustomerOrderService;
import com.washmadam.service.db.request.operate.CustomerOrderOperateParam;
import com.washmadam.service.service.communicate.CustomerOrderCommunicateOperateService;
import com.washmadam.service.service.order.CustomerOrderOperateService;
import com.washmadam.service.service.order.CustomerOrderQueryService;
import com.washmadam.service.service.service.CustomerOrderServiceQueryService;

@Component
public class CustomerOrderCommunicateDomain {
	@Autowired
	CustomerOrderCommunicateOperateService customerOrderCommunicateOperateService;
	@Autowired
	CustomerOrderQueryService customerOrderQueryService;
	@Autowired
	CustomerOrderOperateService customerOrderOperateService;
	@Autowired
	CustomerOrderServiceQueryService customerOrderServiceQueryService;

	public void addServiceCommunicate(CustomerOrderCommunicate requestParam) {
		CustomerOrder customerOrder = customerOrderQueryService.getCustomerOrderById(requestParam.getCustomerOrderId());
		if(customerOrder==null) {
			throw new RuntimeException("订单查询失败");
		}
		//添加沟通记录
		CustomerOrderService customerOrderService = customerOrderServiceQueryService.getCurrentCustomerOrderServiceByOrderId(customerOrder.getId(),(customerOrder.getTotalNum()-customerOrder.getCurrentNum()+1));
		requestParam.setCustomerOrderSerivceId(customerOrderService.getId());
		requestParam.setCustomerId(customerOrder.getCustomerId());
		requestParam.setCustomerName(customerOrderService.getCustomerName());
		requestParam.setCustomerOrderId(customerOrder.getId());
		requestParam.setCommunicateType(CommunicateTypeEnum.SERVICE.getCode());
		requestParam.setCustomerOrderSerivceNum(customerOrderService.getServiceNum());
		customerOrderCommunicateOperateService.saveOrUpdateCustomerOrderCommunicate(requestParam);
		//更新订单状态为预约异常
		CustomerOrderOperateParam customerOrderOperateParam = new CustomerOrderOperateParam();
		customerOrderOperateParam.setId(customerOrder.getId());
		customerOrderOperateParam.setReserveStatus(ReserveStatusEnum.RESERVE_ERROR.getCode());
		customerOrderOperateParam.setUpdateTime(new Date());
		customerOrderOperateParam.setUpdateBy(requestParam.getUpdateBy());
		customerOrderOperateService.saveOrUpdateOrder(customerOrderOperateParam);
	}

	public void addOrderCommunicate(CustomerOrderCommunicate requestParam) {
		CustomerOrder customerOrder = customerOrderQueryService.getCustomerOrderById(requestParam.getCustomerOrderId());
		if(customerOrder==null) {
			throw new RuntimeException("订单查询失败");
		}
		//添加沟通记录
		requestParam.setCustomerId(customerOrder.getCustomerId());
		requestParam.setCustomerName(customerOrder.getCustomerName());
		requestParam.setCustomerOrderId(customerOrder.getId());
		requestParam.setCommunicateType(CommunicateTypeEnum.ORDER.getCode());
		customerOrderCommunicateOperateService.saveOrUpdateCustomerOrderCommunicate(requestParam);
	}


}
